#include <iostream>
#include <vector>
#include <cstdio> 

#define in read()
#define fi first
#define se second
#define pb push_back

using namespace std;

typedef long long ll;
typedef double db;
typedef vector < int >  vec;
typedef pair < int , int > pii;

int read() {
  int x = 0; bool f = 0; char ch = getchar(); while(!isdigit(ch)) f |= ch == '-',ch = getchar();
  while(isdigit(ch)) x = x * 10 + (ch ^ 48),ch = getchar(); return f ? -x : x;
}

const int N = 1e5 + 10;

int n,K[N],ans[N],cnt1[N],cnt2[N],res[N],siz[N],son[N],RT,mxd = 0,s1[N],s2[N],dep[N],mxd2;
vec G[N];
bool vis[N];

void getrt(int x,int p,int tsz) {
    son[x] = 0; siz[x] = 1;
    for(auto y : G[x]) if(y != p && !vis[y]) getrt(y,x,tsz),son[x] = max(son[x],siz[y]),siz[x] += siz[y];
    son[x] = max(son[x],tsz - siz[x]); if(RT == 0 || son[x] < son[RT]) RT = x;
}

void dfs(int x,int p,int *cnt,int &mxd) {
    siz[x] = 1; dep[x] = dep[p] + 1; cnt[dep[x]]++; mxd = max(mxd,dep[x]);
    for(auto y : G[x]) if(y != p && !vis[y]) dfs(y,x,cnt,mxd),siz[x] += siz[y];
}

void calc(int x,int p) {
    int lef = ans[x] - dep[x];
    res[x] += s1[mxd] - (lef < 0 ? 0 : s1[min(mxd,lef)]) - (s2[mxd2] - (lef < 0 ? 0 : s2[min(mxd2,lef)]));
    for(auto y : G[x]) if(y ^ p && !vis[y]) calc(y,x);
}

void Divide(int x,int tsz) {
    RT = 0; getrt(x,0,tsz); x = RT; vis[x] = 1;
    mxd = 0; dfs(x,0,cnt1,mxd);
    s1[0] = 1; for(int i = 1;i <= mxd;i++) s1[i] = s1[i - 1] + cnt1[i];
    res[x] += s1[mxd] - s1[min(ans[x],mxd)];
    for(auto y : G[x])
	if(!vis[y]) {
	    mxd2 = 0; dfs(y,x,cnt2,mxd2);
	    s2[0] = 0; for(int i = 1;i <= mxd2;i++) s2[i] = s2[i - 1] + cnt2[i];
	    calc(y,x); for(int i = 0;i <= mxd2;i++) cnt2[i] = 0;
	}
    for(int i = 0;i <= mxd;i++) cnt1[i] = 0;
    for(auto y : G[x]) if(!vis[y]) Divide(y,siz[y]);
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin); freopen("1.out","w",stdout);
#endif
    n = in; for(int i = 1;i <= n;i++) K[i] = in;
    for(int i = 1,u,v;i < n;i++) u = in,v = in,G[u].pb(v),G[v].pb(u);
    dep[0] = -1;
    for(int bit = 16;bit >= 0;bit--) {
	for(int i = 1;i <= n;i++) ans[i] += 1 << bit,res[i] = vis[i] = 0;
	Divide(1,n);
	for(int i = 1;i <= n;i++) if(res[i] < K[i]) ans[i] -= 1 << bit;
    }
    for(int i = 1;i <= n;i++) printf("%d ",ans[i]); puts("");
} 
